'use strict';
const nodeEval = require('node-eval');
const lodash = require('lodash');
const dayjs = require('dayjs');
const cheerio = require('cheerio');

const unitTime = () => dayjs().valueOf();
const $ = html => cheerio.load(html, { decodeEntities: false });
const getDataByText = (data, attrMap = {}, path = '', handle = 'default') => {
  let text = '';
  Object.keys(attrMap).forEach(name => {
    text += `
        ${attrMap[name]}:${name},
      `;
  });

  if (handle !== 'default') {
    data = data.replace(handle + '(', 'var data = ')
      .replace(');', '');
  }
  const res = nodeEval(`${data};module.exports = {${text}};`);

  return path === '' ? res : lodash.get(res, path);
};

module.exports = {
  unitTime,
  $,
  getDataByText
};
